package com.freeter.modules.gjs.service;

import com.freeter.modules.gjs.util.SendMailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.freeter.modules.gjs.mapper.TransferRequestTokenLinkMapper;

import javax.mail.MessagingException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 库存调拨请求提醒
 */
@Component
public class TransferRequestComponent {

    @Value("${mail.office365.from}")
    private String mailFrom;
    @Value("${mail.office365.pass}")
    private String mailPass;

    @Autowired
    private TransferRequestTokenLinkMapper tokenLinkMapper;

    public void execute()  {
        List<Map<String, Object>> users = tokenLinkMapper.selectAlertUsers();
        for (Map<String, Object> user : users) {
            String email = (String) user.get("email");
            try {
                List<Map<String, Object>> items = tokenLinkMapper.selectAlertItemsByEmail(email);
                if (items != null && !items.isEmpty()) {
                    String subject = "[IMS] Approved Stock Transfer Request Alert";
                    StringBuilder body = new StringBuilder("Following approved stock transfer request(s) has/have not been completed in 24 hrs:<br />");
                    for (Map<String, Object> item : items) {
                        body.append(item.get("item_desc")).append(" (");
                        body.append("Job No.: ").append(item.get("job_no")).append(", ");
                        body.append("Requester: ").append(item.get("requester")).append(", ");
                        body.append("Qty: ").append(item.get("qty")).append(" ").append(item.getOrDefault("uom", "")).append(", ");
                        body.append("(From) Project/Site: ").append(item.get("from_project_code"));
                        if (item.get("from_site_info") != null) body.append("-").append(item.get("from_site_info"));
                        body.append(", (To) Project/Site: ").append(item.get("to_project_code"));
                        if (item.get("to_site_info") != null) body.append("-").append(item.get("to_site_info"));
                        body.append(")<br />");
                    }
                    SendMailUtil.send365Email(email, subject, body.toString(), mailFrom, mailPass);
                    List<Long> requestIds = items.stream().map(i -> ((Number)i.get("id")).longValue()).collect(Collectors.toList());
                    tokenLinkMapper.updateApproverAlertSent(requestIds, email);
                }
            } catch (MessagingException e) {
                System.err.println("Transfer Request Job Alert: Failed send mail to " + email);
                e.printStackTrace();
            } catch (Exception e) {
                System.err.println("Transfer Request Job Alert: An error occurred: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }

}
